<?php
// +----------------------------------------------------------------------
// | HisiPHP框架[基于ThinkPHP5开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2018 http://www.hisiphp.com
// +----------------------------------------------------------------------
// | HisiPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: 橘子俊 <364666827@qq.com>，开发者QQ群：50304283
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\util\Dir;
use app\admin\model\Article as ArticleModel;
/**
 * 后台默认首页控制器
 * @package app\admin\controller
 */

class Index extends Admin
{
    /**
     * 首页
     * @author 橘子俊 <364666827@qq.com>
     * @return mixed
     */
    public function index()
    {
        if (cookie('hisi_iframe')) {
            $this->view->engine->layout(false);
            return $this->fetch('iframe');
        } else {
            return $this->fetch();
        }
    }

    /**
     * 欢迎首页
     * @author 橘子俊 <364666827@qq.com>
     * @return mixed
     */
    public function welcome()
    {
        return $this->fetch('index');
    }

    public function article(){
/*        //var_dump($this->request->isAjax());
        //echo 111;exit;
        //if ($this->request->isAjax()) {
            $where = $data = [];
            $page = input('param.page/d', 1);
            $limit = input('param.limit/d', 15);
            $where['classid'] = 1;
            $data['data'] = ArticleModel::with('classify')->where($where)->page($page)->limit($limit)->select();
            //print_r($data);exit;
            $data['count'] = ArticleModel::where($where)->count('id');
            $data['code'] = 0;
            $data['msg'] = '';
            //print_r($data);exit;
            //return json($data);
        //}
        $classify = ['1' => '图文','2' => '单图' ,'3' => '纯文'];
        $this->assign('data_list', $data['data']);
        $this->assign('classify', $classify);
        return $this->fetch();
        */
        $where = "  1 = 1 ";
        $phone = input('q');
        if($phone){
            $where .= " and teltphone = '$phone'";
        }
        $cid = input('cid');
        if($cid){
            $where .= " and classid = $cid";
        }
        $result = db('article')
            ->where($where)
            ->order("id desc")
            ->paginate(10, false, ['query' => input('get.')]);

        $pages = $result->render();

        $classify_list = db('classify')->select();
        $classify = [];
        if($classify_list){
            foreach($classify_list as $key => $info){
                $classify[$info['id']] = $info['name'];
            }
        }
        //$classify = ['1' => '图文','2' => '单图' ,'3' => '纯文'];
        $this->assign('data_list', $result);
        $this->assign('classify', $classify);
        $this->assign("pages", $pages);
        return $this->fetch();
    }

    //分类管理
    public function classify(){
        $result = db('classify')
            ->order("id asc")
            ->paginate(10, false, ['query' => input('get.')]);

        $pages = $result->render();
        $this->assign('data_list', $result);
        $this->assign("pages", $pages);
        return $this->fetch();
    }


    /**
     * 清理缓存
     * @author 橘子俊 <364666827@qq.com>
     * @return mixed
     */
    public function clear()
    {
        if (Dir::delDir(RUNTIME_PATH) === false) {
            return $this->error('缓存清理失败！');
        }
        return $this->success('缓存清理成功！');
    }
}
